package com.jasmine.bjstkserver.server.article.draft;

import cn.hutool.core.util.StrUtil;
import com.jasmine.bjstkserver.mapper.ArticleDraftMapper;
import com.jasmine.bjstkserver.server.article.ArticleUtil;
import com.jasmine.bjstkserver.server.article.draft.model.ArticleDraftEntity;
import com.jasmine.bjstkserver.server.article.draft.model.ArticleDraftRes;
import com.jasmine.bjstkserver.server.article.online.model.ArticleReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 文章草稿
 *
 * @author jasmineXz
 */
@Service
public class ArticleDraftServiceImpl implements ArticleDraftService {
    private static final Logger log = LoggerFactory.getLogger(ArticleDraftServiceImpl.class);

    @Autowired
    private ArticleDraftMapper articleDraftMapper;

    /**
     * 草稿列表
     * @param article 文件夹ID等条件
     * @return 草稿列表
     */
    @Override
    public List<ArticleDraftRes> findList(ArticleReq article) {
        List<ArticleDraftRes> list = articleDraftMapper.findList(article);
        list.forEach(item -> {
            item.setContentHtml(StrUtil.sub(item.getContentHtml(),0,500));
        });
        return list;
    }


    /**
     * 查询文章内容
     * @param article 查询条件
     *  article#showType {
     *      ALL  : 不显示文章内容
     *      MD   : 显示markdown内容
     *      HTML : 显示html内容
     *  }
     * @return 文章信息,包含发布文章的点击信息,会与article表联查
     */
    @Override
    public ArticleDraftRes findOne(ArticleReq article) {
        return articleDraftMapper.findOne(article);
    }


    /**
     * 新增文章
     * @param article 文章信息
     * @return 文章编号
     */
    @Override
    public Integer insert(ArticleDraftEntity article) {
        ArticleUtil.paramCheck(article);
        article.setContentHtml(null);
        article.setContentMd(null);
        article.setCreTime(LocalDateTime.now());
        article.setUpdTime(LocalDateTime.now());
        return articleDraftMapper.insertSelective(article);
    }


    /**
     * 修改文章信息,不修改文章内容
     * @param article 文章信息
     * @return 影响行数
     */
    @Override
    public Integer updateInfo(ArticleDraftEntity article) {
        article.setContentMd(null);
        article.setContentHtml(null);
        article.setCreTime(null);
        return articleDraftMapper.updateByPrimaryKeySelective(article);
    }

    private static char[] replaceStr = new char[] {' ','\n','\r'};

    /**
     * 修改文章内容,不修改文章信息
     * @param article 文章信息
     * @return 影响行数
     */
    @Override
    public Integer updateContent(ArticleDraftEntity article) {
        log.info("[BJSTK] 修改文章,版本 : {}",article.getVersion());
        ArticleDraftEntity upd = new ArticleDraftEntity();
        upd.setArticleId(article.getArticleId());
        upd.setContentMd(article.getContentMd());
        upd.setContentHtml(article.getContentHtml());
        upd.setVersion(article.getVersion());
        upd.setWords(StrUtil.replaceChars(article.getContentMd(),replaceStr,"").length());
        upd.setUpdTime(LocalDateTime.now());
        return articleDraftMapper.updateByPrimaryKeySelective(upd);
    }

    @Override
    public Integer resetFolder(Integer userId, Integer folderId) {
        return articleDraftMapper.resetFolder(userId,folderId);
    }


    /**
     * 软删除文章
     * @param articleId 文章ID
     * @return 影响行数
     */
    @Override
    public Integer delete(Integer articleId) {
        ArticleDraftEntity draft = new ArticleDraftEntity();
        draft.setArticleId(articleId);
        draft.setDelState(LocalDateTime.now().toString());
        return articleDraftMapper.updateByPrimaryKeySelective(draft);
    }




    /* ------------------- 统计 ------------------- */


    /**
     * 文章统计
     * @param article 用户
     * @return 文章数,字数,点击数,阅读数
     */
    @Override
    public Map<String, Long> statistics(ArticleReq article) {
        return articleDraftMapper.statistics(article);
    }
}
